import os
import shutil
from pathlib import Path

#重命名并合并文件夹内容
def rename_and_copy_images(src_dir, dst_dir, prefix="image", startnum = 0):
    """
    将源文件夹中的图片复制到目标文件夹，并按顺序重命名

    参数:
        src_dir (str): 源图片文件夹路径
        dst_dir (str): 目标文件夹路径
        prefix (str): 新文件名前缀 (默认为"image")
    """
    # 创建目标文件夹（如果不存在）
    Path(dst_dir).mkdir(parents=True, exist_ok=True)

    # 支持的图片扩展名
    image_exts = ('.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff', '.webp')

    # 获取源文件夹中所有图片文件
    image_files = [
        f for f in os.listdir(src_dir)
        if f.lower().endswith(image_exts) and os.path.isfile(os.path.join(src_dir, f))
    ]

    # 按文件名排序
    image_files.sort()

    # 复制并重命名文件
    for i, filename in enumerate(image_files, start=1):
        # 获取文件扩展名
        ext = os.path.splitext(filename)[1].lower()

        # 生成新文件名 (例如：image_001.jpg)
        new_name = f"{prefix}_{i + startnum:04d}{ext}"

        # 构建完整路径
        src_path = os.path.join(src_dir, filename)
        dst_path = os.path.join(dst_dir, new_name)

        # 复制文件
        shutil.copy2(src_path, dst_path)
        print(f"已复制: {filename} -> {new_name}")

    print(f"\n操作完成！共处理 {len(image_files)} 张图片。")
    return len(image_files)


if __name__ == "__main__":
    # 配置路径
    destination_folder = r"D:\output\vcg_photos"  # 替换为目标文件夹路径

    currentNumber = 0

    # 执行操作
    currentNumber += rename_and_copy_images(r"D:\output\vcg_photos_result", destination_folder, "image", currentNumber)
    print("unsplash_photos_completed")
    currentNumber += rename_and_copy_images(r"D:\output\9", destination_folder, "image", currentNumber)
    print("unsplash_photos_completed")
    currentNumber += rename_and_copy_images(r"D:\output\10", destination_folder, "image", currentNumber)
    print("unsplash_photos_completed")
    currentNumber += rename_and_copy_images(r"D:\output\11", destination_folder, "image", currentNumber)
    print("unsplash_photos_completed")
    currentNumber += rename_and_copy_images(r"D:\output\12", destination_folder, "image", currentNumber)
    print("unsplash_photos_completed")
    currentNumber += rename_and_copy_images(r"D:\output\13", destination_folder, "image", currentNumber)
    print("unsplash_photos_completed")
    # currentNumber += rename_and_copy_images(r"D:\output\6", destination_folder, "image", currentNumber)
    # print("unsplash_photos_completed")
    # currentNumber += rename_and_copy_images(r"D:\output\7", destination_folder, "image", currentNumber)
    # print("unsplash_photos_completed")
    # currentNumber += rename_and_copy_images(r"D:\output\8", destination_folder, "image", currentNumber)
    # print("unsplash_photos_completed")